package com.kkwrite.regimen.consumer.controller.spokesman;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.spokesman.SpokesmanDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/** 
 * 项目用户代言
 *
 * @author jinying
 * 
 */
@Api(value = "项目用户代言接口")
@RestController
@RequestMapping("/spokesman")
public class SpokesmanController {

	private static final Logger logger = LoggerFactory.getLogger(SpokesmanController.class);
	
	@Resource(name = "restTemplate")
	private RestTemplate restTemplate;
	
	@ApiOperation(value = "根据项目编号查询项目用户代言")
	@ApiImplicitParams(
        {
            @ApiImplicitParam(name = "itemId", value = "项目编号", required = true, paramType = "path", dataType = "String")
        }
    )
	@SuppressWarnings("rawtypes")
	@GetMapping("/getSpokesman/{itemId}")
	public OutDTO<List<SpokesmanDTO>> getSpokesman(@PathVariable String itemId) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpokesmanController.getSpokesman(), itemId = {}", itemId);
		}
		OutDTO outDTO = null;
		try {
			String url = Constant.SERVICE_PREFIX + "/spokesman/getSpokesman/" + itemId;
			outDTO = restTemplate.getForObject(url, OutDTO.class);
			return outDTO;
		} catch (RestClientException e) {
			logger.error("查询代言人出错，itemId = " + itemId + "_" + e.getMessage());
			e.printStackTrace();
			return new OutDTO<List<SpokesmanDTO>>("failed", e.getMessage());
		}
	}
}
